



<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#FFF">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon.png">

<link rel="icon" type="image/ico" sizes="32x32" href="/blog/images/favicon.ico">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">


<link rel="alternate" type="application/rss+xml" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/rss.xml" />
<link rel="alternate" type="application/atom+xml" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/atom.xml" />
<link rel="alternate" type="application/json" title="凡尘一叶" href="http://fyduyu.gitee.io/blog/feed.json" />

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Mulish:300,300italic,400,400italic,700,700italic%7CFredericka%20the%20Great:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20JP:300,300italic,400,400italic,700,700italic%7CNoto%20Serif%20SC:300,300italic,400,400italic,700,700italic%7CInconsolata:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="/blog/css/app.css?v=0.2.5">

  
  <meta name="keywords" content="分布式" />


<link rel="canonical" href="http://fyduyu.gitee.io/blog/2020/12/24/gateway/">



  <title>
gateway - SpringCloud |
blog = 凡尘一叶</title>
<meta name="generator" content="Hexo 5.3.0"></head>
<body itemscope itemtype="http://schema.org/WebPage">
  <div id="loading">
    <div class="cat">
      <div class="body"></div>
      <div class="head">
        <div class="face"></div>
      </div>
      <div class="foot">
        <div class="tummy-end"></div>
        <div class="bottom"></div>
        <div class="legs left"></div>
        <div class="legs right"></div>
      </div>
      <div class="paw">
        <div class="hands left"></div>
        <div class="hands right"></div>
      </div>
    </div>
  </div>
  <div id="container">
    <header id="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="inner">
        <div id="brand">
          <div class="pjax">
          
  <h1 itemprop="name headline">gateway
  </h1>
  
<div class="meta">
  <span class="item" title="创建时间：2020-12-24 14:51:28">
    <span class="icon">
      <i class="ic i-calendar"></i>
    </span>
    <span class="text">发表于</span>
    <time itemprop="dateCreated datePublished" datetime="2020-12-24T14:51:28+08:00">2020-12-24</time>
  </span>
  <span class="item" title="本文字数">
    <span class="icon">
      <i class="ic i-pen"></i>
    </span>
    <span class="text">本文字数</span>
    <span>4.6k</span>
    <span class="text">字</span>
  </span>
  <span class="item" title="阅读时长">
    <span class="icon">
      <i class="ic i-clock"></i>
    </span>
    <span class="text">阅读时长</span>
    <span>4 分钟</span>
  </span>
</div>


          </div>
        </div>
        <nav id="nav">
  <div class="inner">
    <div class="toggle">
      <div class="lines" aria-label="切换导航栏">
        <span class="line"></span>
        <span class="line"></span>
        <span class="line"></span>
      </div>
    </div>
    <ul class="menu">
      <li class="item title"><a href="/blog/" rel="start">blog</a></li>
    </ul>
    <ul class="right">
      <li class="item theme">
        <i class="ic i-sun"></i>
      </li>
      <li class="item search">
        <i class="ic i-search"></i>
      </li>
    </ul>
  </div>
</nav>

      </div>
      <div id="imgs" class="pjax">
        <ul>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gipeudstjqj20zk0m8k3r.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gicmnywqgpj20zk0m8dwx.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1giclwrdwyaj20zk0m8are.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1giclgrvbd6j20zk0m8qv5.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gipevarprfj20zk0m8npd.jpg"></li>
          <li class="item" data-background-image="https://tva4.sinaimg.cn/large/6833939bly1gicliierfjj20zk0m8npd.jpg"></li>
        </ul>
      </div>
    </header>
    <div id="waves">
      <svg class="waves" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto">
        <defs>
          <path id="gentle-wave" d="M-160 44c30 0 58-18 88-18s 58 18 88 18 58-18 88-18 58 18 88 18 v44h-352z" />
        </defs>
        <g class="parallax">
          <use xlink:href="#gentle-wave" x="48" y="0" />
          <use xlink:href="#gentle-wave" x="48" y="3" />
          <use xlink:href="#gentle-wave" x="48" y="5" />
          <use xlink:href="#gentle-wave" x="48" y="7" />
        </g>
      </svg>
    </div>
    <main>
      <div class="inner">
        <div id="main" class="pjax">
          
  <div class="article wrap">
    
<div class="breadcrumb" itemscope itemtype="https://schema.org/BreadcrumbList">
<i class="ic i-home"></i>
<span><a href="/blog/">首页</a></span><i class="ic i-angle-right"></i>
<span  class="current" itemprop="itemListElement" itemscope itemtype="https://schema.org/ListItem"><a href="/blog/categories/SpringCloud/" itemprop="item" rel="index" title="分类于 SpringCloud"><span itemprop="name">SpringCloud</span></a>
<meta itemprop="position" content="1" /></span>
</div>

    <article itemscope itemtype="http://schema.org/Article" class="post block" lang="zh-CN">
  <link itemprop="mainEntityOfPage" href="http://fyduyu.gitee.io/blog/2020/12/24/gateway/">

  <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
    <meta itemprop="image" content="/blog/images/touxiang.jpg">
    <meta itemprop="name" content="杜朝宇">
    <meta itemprop="description" content=", 个人博客&编程笔记">
  </span>

  <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
    <meta itemprop="name" content="凡尘一叶">
  </span>

  <div class="body md" itemprop="articleBody">
    

    <h1 id="介绍"><a class="markdownIt-Anchor" href="#介绍">#</a> 介绍</h1>
<p>gateway 之所以性能号，因为底层使用 WebFlux, 而 webFlux 底层使用 netty 通信 (NIO)</p>
<p>什么是 webflux:</p>
<p>是一个非阻塞的 web 框架，类似 springmvc 这样的</p>
<p>GateWay 的一些概念:</p>
<p>1, 路由：就是根据某些规则，将请求发送到指定服务上</p>
<p>2, 断言：就是判断，如果符合条件就是 xxxx, 反之 yyyy</p>
<p>3, 过滤：路由前后，过滤请求</p>
<h1 id="使用gateway"><a class="markdownIt-Anchor" href="#使用gateway">#</a> 使用 GateWay</h1>
<p>想要新建一个 GateWay 的项目</p>
<p>名字: 	cloud-gateway-9527</p>
<h2 id="pom"><a class="markdownIt-Anchor" href="#pom">#</a> pom</h2>
<figure class="highlight xml"><figcaption data-lang="XML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token comment">&lt;!--gateway--></span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-gateway<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token comment">&lt;!--eureka client--></span></pre></td></tr><tr><td data-num="7"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">></span></span>org.springframework.cloud<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="9"></td><td><pre>    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">></span></span>spring-cloud-starter-netflix-eureka-client<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">></span></span></pre></td></tr><tr><td data-num="10"></td><td><pre><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">></span></span></pre></td></tr></table></figure><h2 id="配置文件"><a class="markdownIt-Anchor" href="#配置文件">#</a> 配置文件</h2>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">server</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">9527</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway</pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>  <span class="token key atrule">instance</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>    <span class="token key atrule">hostname</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway<span class="token punctuation">-</span>service</pre></td></tr><tr><td data-num="9"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="11"></td><td><pre>    <span class="token key atrule">fetch-registry</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="12"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="13"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka</pre></td></tr></table></figure><h2 id="主启动类"><a class="markdownIt-Anchor" href="#主启动类">#</a> 主启动类</h2>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token annotation punctuation">@EnableEurekaClient</span></pre></td></tr></table></figure><h2 id="针对pay模块设置路由"><a class="markdownIt-Anchor" href="#针对pay模块设置路由">#</a> 针对 pay 模块，设置路由</h2>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway</pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">gateway</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="6"></td><td><pre>      <span class="token key atrule">routes</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> payment_routh</pre></td></tr><tr><td data-num="8"></td><td><pre>          <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">8001</span></pre></td></tr><tr><td data-num="9"></td><td><pre>          <span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>            <span class="token punctuation">-</span> Path=/payment/get/<span class="token important">**</span></pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> payment_routh</pre></td></tr><tr><td data-num="12"></td><td><pre>          <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span><span class="token number">8001</span></pre></td></tr><tr><td data-num="13"></td><td><pre>          <span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="14"></td><td><pre>            <span class="token punctuation">-</span> Path=/payment/lb/<span class="token important">**</span></pre></td></tr></table></figure><h2 id="开始测试"><a class="markdownIt-Anchor" href="#开始测试">#</a> 开始测试</h2>
<p>如果启动 GateWay 报错<br>
可能是 GateWay 模块引入了 web 和监控的 starter 依赖，需要移除</p>
<p>访问:</p>
<p>​		localhost:9527/payment/get/1</p>
<h2 id="通过编码方式配置"><a class="markdownIt-Anchor" href="#通过编码方式配置">#</a> 通过编码方式配置</h2>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@SpringBootApplication</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DemogatewayApplication</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="3"></td><td><pre>	<span class="token annotation punctuation">@Bean</span></pre></td></tr><tr><td data-num="4"></td><td><pre>	<span class="token keyword">public</span> <span class="token class-name">RouteLocator</span> <span class="token function">customRouteLocator</span><span class="token punctuation">(</span><span class="token class-name">RouteLocatorBuilder</span> builder<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="5"></td><td><pre>		<span class="token keyword">return</span> builder<span class="token punctuation">.</span><span class="token function">routes</span><span class="token punctuation">(</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="6"></td><td><pre>			<span class="token punctuation">.</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string">"path_route"</span><span class="token punctuation">,</span> r <span class="token operator">-></span> r<span class="token punctuation">.</span><span class="token function">path</span><span class="token punctuation">(</span><span class="token string">"/get"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="7"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">uri</span><span class="token punctuation">(</span><span class="token string">"http://httpbin.org"</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="8"></td><td><pre>			<span class="token punctuation">.</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string">"host_route"</span><span class="token punctuation">,</span> r <span class="token operator">-></span> r<span class="token punctuation">.</span><span class="token function">host</span><span class="token punctuation">(</span><span class="token string">"*.myhost.org"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="9"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">uri</span><span class="token punctuation">(</span><span class="token string">"http://httpbin.org"</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="10"></td><td><pre>			<span class="token punctuation">.</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string">"rewrite_route"</span><span class="token punctuation">,</span> r <span class="token operator">-></span> r<span class="token punctuation">.</span><span class="token function">host</span><span class="token punctuation">(</span><span class="token string">"*.rewrite.org"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="11"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">filters</span><span class="token punctuation">(</span>f <span class="token operator">-></span> f<span class="token punctuation">.</span><span class="token function">rewritePath</span><span class="token punctuation">(</span><span class="token string">"/foo/(?&lt;segment>.*)"</span><span class="token punctuation">,</span> <span class="token string">"/$&#123;segment&#125;"</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="12"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">uri</span><span class="token punctuation">(</span><span class="token string">"http://httpbin.org"</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="13"></td><td><pre>			<span class="token punctuation">.</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string">"hystrix_route"</span><span class="token punctuation">,</span> r <span class="token operator">-></span> r<span class="token punctuation">.</span><span class="token function">host</span><span class="token punctuation">(</span><span class="token string">"*.hystrix.org"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="14"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">filters</span><span class="token punctuation">(</span>f <span class="token operator">-></span> f<span class="token punctuation">.</span><span class="token function">hystrix</span><span class="token punctuation">(</span>c <span class="token operator">-></span> c<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">"slowcmd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="15"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">uri</span><span class="token punctuation">(</span><span class="token string">"http://httpbin.org"</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="16"></td><td><pre>			<span class="token punctuation">.</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string">"hystrix_fallback_route"</span><span class="token punctuation">,</span> r <span class="token operator">-></span> r<span class="token punctuation">.</span><span class="token function">host</span><span class="token punctuation">(</span><span class="token string">"*.hystrixfallback.org"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="17"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">filters</span><span class="token punctuation">(</span>f <span class="token operator">-></span> f<span class="token punctuation">.</span><span class="token function">hystrix</span><span class="token punctuation">(</span>c <span class="token operator">-></span> c<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">"slowcmd"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setFallbackUri</span><span class="token punctuation">(</span><span class="token string">"forward:/hystrixfallback"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="18"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">uri</span><span class="token punctuation">(</span><span class="token string">"http://httpbin.org"</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="19"></td><td><pre>			<span class="token punctuation">.</span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string">"limit_route"</span><span class="token punctuation">,</span> r <span class="token operator">-></span> r</pre></td></tr><tr><td data-num="20"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">host</span><span class="token punctuation">(</span><span class="token string">"*.limited.org"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">and</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">path</span><span class="token punctuation">(</span><span class="token string">"/anything/**"</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="21"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">filters</span><span class="token punctuation">(</span>f <span class="token operator">-></span> f<span class="token punctuation">.</span><span class="token function">requestRateLimiter</span><span class="token punctuation">(</span>c <span class="token operator">-></span> c<span class="token punctuation">.</span><span class="token function">setRateLimiter</span><span class="token punctuation">(</span><span class="token function">redisRateLimiter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="22"></td><td><pre>				<span class="token punctuation">.</span><span class="token function">uri</span><span class="token punctuation">(</span><span class="token string">"http://httpbin.org"</span><span class="token punctuation">)</span><span class="token punctuation">)</span></pre></td></tr><tr><td data-num="23"></td><td><pre>			<span class="token punctuation">.</span><span class="token function">build</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="24"></td><td><pre>	<span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="25"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><h1 id="重构"><a class="markdownIt-Anchor" href="#重构">#</a> 重构:</h1>
<p>上面的配置虽然首先了网关，但是是在配置文件中写死了要路由的地址</p>
<p>现在需要修改，不指定地址，而是根据微服务名字进行路由，我们可以在注册中心获取某组微服务的地址</p>
<p>需要:</p>
<p>​		1 个 eureka,2 个 pay 模块</p>
<h2 id="修改gateway模块的配置文件"><a class="markdownIt-Anchor" href="#修改gateway模块的配置文件">#</a> 修改 GateWay 模块的配置文件:</h2>
<figure class="highlight yml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">application</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">name</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway</pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">gateway</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="6"></td><td><pre>      <span class="token key atrule">discovery</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token key atrule">locator</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>          <span class="token key atrule">enabled</span><span class="token punctuation">:</span> <span class="token boolean important">true</span> <span class="token comment">#开启从注册中心动态创建路由的功能，利用微服务名进行路由</span></pre></td></tr><tr><td data-num="9"></td><td><pre>      <span class="token key atrule">routes</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="10"></td><td><pre>        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> payment_routh</pre></td></tr><tr><td data-num="11"></td><td><pre>          <span class="token comment">#uri: http://localhost:8001</span></pre></td></tr><tr><td data-num="12"></td><td><pre>          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//cloud<span class="token punctuation">-</span>payment<span class="token punctuation">-</span>service <span class="token comment">#匹配后提供服务的路由地址</span></pre></td></tr><tr><td data-num="13"></td><td><pre>          <span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="14"></td><td><pre>            <span class="token punctuation">-</span> Path=/payment/get/<span class="token important">**</span></pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> payment_routh</pre></td></tr><tr><td data-num="16"></td><td><pre>          <span class="token comment">#uri: http://localhost:8001</span></pre></td></tr><tr><td data-num="17"></td><td><pre>          <span class="token key atrule">uri</span><span class="token punctuation">:</span> lb<span class="token punctuation">:</span>//cloud<span class="token punctuation">-</span>payment<span class="token punctuation">-</span>service <span class="token comment">#匹配后提供服务的路由地址</span></pre></td></tr><tr><td data-num="18"></td><td><pre>          <span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="19"></td><td><pre>            <span class="token punctuation">-</span> Path=/payment/lb/<span class="token important">**</span></pre></td></tr></table></figure><p>注册进 Eureka</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">eureka</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">instance</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">hostname</span><span class="token punctuation">:</span> cloud<span class="token punctuation">-</span>gateway<span class="token punctuation">-</span>service</pre></td></tr><tr><td data-num="4"></td><td><pre>  <span class="token key atrule">client</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token key atrule">register-with-eureka</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="6"></td><td><pre>    <span class="token key atrule">fetch-registry</span><span class="token punctuation">:</span> <span class="token boolean important">true</span></pre></td></tr><tr><td data-num="7"></td><td><pre>    <span class="token key atrule">service-url</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>      <span class="token key atrule">defaultZone</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//eureka7001.com<span class="token punctuation">:</span>7001/eureka</pre></td></tr></table></figure><h1 id="pridicate断言"><a class="markdownIt-Anchor" href="#pridicate断言">#</a> Pridicate 断言:</h1>
<h2 id="通过时间匹配"><a class="markdownIt-Anchor" href="#通过时间匹配">#</a> 通过时间匹配</h2>
<p>Predicate 支持设置一个时间，在请求进行转发的时候，可以通过判断在这个时间之前或者之后进行转发。比如我们现在设置只有在 2019 年 1 月 1 日才会转发到我的网站，在这之前不进行转发，我就可以这样配置：</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>		  <span class="token punctuation">-</span> After=2018<span class="token punctuation">-</span>01<span class="token punctuation">-</span>20T06<span class="token punctuation">:</span>06<span class="token punctuation">:</span>06+08<span class="token punctuation">:</span>00<span class="token punctuation">[</span>Asia/Shanghai<span class="token punctuation">]</span></pre></td></tr><tr><td data-num="3"></td><td><pre>          <span class="token punctuation">-</span> Before=2018<span class="token punctuation">-</span>01<span class="token punctuation">-</span>20T06<span class="token punctuation">:</span>06<span class="token punctuation">:</span>06+08<span class="token punctuation">:</span>00<span class="token punctuation">[</span>Asia/Shanghai<span class="token punctuation">]</span></pre></td></tr></table></figure><h2 id="通过-cookie和请求头等等-匹配"><a class="markdownIt-Anchor" href="#通过-cookie和请求头等等-匹配">#</a> 通过 Cookie 和请求头等等 匹配</h2>
<p>Cookie Route Predicate 可以接收两个参数，一个是 Cookie name , 一个是正则表达式，路由规则会通过获取对应的 Cookie name 值和正则表达式去匹配，如果匹配上就会执行路由，如果没有匹配上则不执行。</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>         <span class="token punctuation">-</span> Cookie=ityouknow<span class="token punctuation">,</span> kee.e</pre></td></tr><tr><td data-num="3"></td><td><pre>         <span class="token punctuation">-</span> Header=X<span class="token punctuation">-</span>Request<span class="token punctuation">-</span>Id<span class="token punctuation">,</span> \d+</pre></td></tr></table></figure><p>其他</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token punctuation">-</span> Host=<span class="token important">**.ityouknow.com</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token punctuation">-</span> Method=GET  <span class="token comment">#通过请求方式匹配</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token punctuation">-</span> Path=/foo/<span class="token punctuation">&#123;</span>segment<span class="token punctuation">&#125;</span> <span class="token comment">#通过请求路径匹配</span></pre></td></tr><tr><td data-num="4"></td><td><pre><span class="token punctuation">-</span> Query=smile <span class="token comment">#通过请求参数匹配</span></pre></td></tr><tr><td data-num="5"></td><td><pre><span class="token punctuation">-</span> Query=keep<span class="token punctuation">,</span> pu.</pre></td></tr><tr><td data-num="6"></td><td><pre><span class="token punctuation">-</span> RemoteAddr=192.168.1.1/24 通过请求 ip 地址进行匹配</pre></td></tr></table></figure><p>组合使用</p>
<p>After:<br>
 可以指定，只有在指定时间后，才可以路由到指定微服务</p>
<p>before:<br>
 与 after 类似，他说在指定时间之前的才可以访问<br>
 between:<br>
 需要指定两个时间，在他们之间的时间才可以访问</p>
<p>cookie:<br>
 只有包含某些指定 cookie (key,value), 的请求才可以路由</p>
<p>Header:<br>
 只有包含指定请求头的请求，才可以路由</p>
<p>host:<br>
 只有指定主机的才可以访问，<br>
<span class="exturl" data-url="aHR0cDovL3huLS13d3ctb3Q5ZG44bTI0Y3V5anlyZDZ3c3N1Z290bWg1cW55NmFhODQ3bjcwcS5hYS5jb20="> 比如我们当前的网站的域名是 www.aa.com</span><br>
 那么这里就可以设置，只有用户是 www.aa.com 的请求，才进行路由</p>
<p>method:<br>
 只有指定请求才可以路由，比如 get 请求…</p>
<p>path:<br>
 只有访问指定路径，才进行路由<br>
比如访问，/abc 才路由</p>
<p>Query:<br>
 必须带有请求参数才可以访问</p>
<figure class="highlight yaml"><figcaption data-lang="YAML"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token key atrule">spring</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="2"></td><td><pre>  <span class="token key atrule">cloud</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="3"></td><td><pre>    <span class="token key atrule">gateway</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="4"></td><td><pre>      <span class="token key atrule">routes</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="5"></td><td><pre>       <span class="token punctuation">-</span> <span class="token key atrule">id</span><span class="token punctuation">:</span> host_foo_path_headers_to_httpbin</pre></td></tr><tr><td data-num="6"></td><td><pre>        <span class="token key atrule">uri</span><span class="token punctuation">:</span> http<span class="token punctuation">:</span>//ityouknow.com</pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token key atrule">predicates</span><span class="token punctuation">:</span></pre></td></tr><tr><td data-num="8"></td><td><pre>        <span class="token punctuation">-</span> Host=<span class="token important">**.foo.org</span></pre></td></tr><tr><td data-num="9"></td><td><pre>        <span class="token punctuation">-</span> Path=/headers</pre></td></tr><tr><td data-num="10"></td><td><pre>        <span class="token punctuation">-</span> Method=GET</pre></td></tr><tr><td data-num="11"></td><td><pre>        <span class="token punctuation">-</span> Header=X<span class="token punctuation">-</span>Request<span class="token punctuation">-</span>Id<span class="token punctuation">,</span> \d+</pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token punctuation">-</span> Query=foo<span class="token punctuation">,</span> ba.</pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token punctuation">-</span> Query=baz</pre></td></tr><tr><td data-num="14"></td><td><pre>        <span class="token punctuation">-</span> Cookie=chocolate<span class="token punctuation">,</span> ch.p</pre></td></tr><tr><td data-num="15"></td><td><pre>        <span class="token punctuation">-</span> After=2018<span class="token punctuation">-</span>01<span class="token punctuation">-</span>20T06<span class="token punctuation">:</span>06<span class="token punctuation">:</span>06+08<span class="token punctuation">:</span>00<span class="token punctuation">[</span>Asia/Shanghai<span class="token punctuation">]</span></pre></td></tr></table></figure><h1 id="filter过滤器"><a class="markdownIt-Anchor" href="#filter过滤器">#</a> Filter 过滤器:</h1>
<h2 id="生命周期"><a class="markdownIt-Anchor" href="#生命周期">#</a> 生命周期:</h2>
<p>在请求进入路由之前，和处理请求完成，再次到达路由之前</p>
<h2 id="种类"><a class="markdownIt-Anchor" href="#种类">#</a> 种类:</h2>
<ul>
<li>GateWayFilter, 单一的过滤器</li>
<li>GlobalFilter, 全局过滤器:</li>
</ul>
<p>与断言类似，比如闲置，请求头，只有特定的请求头才放行，反之就过滤:</p>
<h2 id="自定义过滤器"><a class="markdownIt-Anchor" href="#自定义过滤器">#</a> 自定义过滤器:</h2>
<p>实现两个接口:</p>
<p>GlobalFilter,Ordered</p>
<figure class="highlight java"><figcaption data-lang="java"></figcaption><table><tr><td data-num="1"></td><td><pre><span class="token annotation punctuation">@Component</span></pre></td></tr><tr><td data-num="2"></td><td><pre><span class="token annotation punctuation">@Slf4j</span></pre></td></tr><tr><td data-num="3"></td><td><pre><span class="token keyword">class</span> <span class="token class-name">MyLogGateWayFilter</span> <span class="token keyword">implements</span> <span class="token class-name">GlobalFilter</span><span class="token punctuation">,</span> <span class="token class-name">Ordered</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="4"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="5"></td><td><pre>    <span class="token keyword">public</span> <span class="token class-name">Mono</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Void</span><span class="token punctuation">></span></span> <span class="token function">filter</span><span class="token punctuation">(</span><span class="token class-name">ServerWebExchange</span> exchange<span class="token punctuation">,</span> <span class="token class-name">GatewayFilterChain</span> chain<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="6"></td><td><pre>        log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"*****************com in MyLogGateWayFilter:    "</span><span class="token operator">+</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="7"></td><td><pre>        <span class="token class-name">String</span> username <span class="token operator">=</span> exchange<span class="token punctuation">.</span><span class="token function">getRequest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getQueryParams</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getFirst</span><span class="token punctuation">(</span><span class="token string">"username"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="8"></td><td><pre>        <span class="token keyword">if</span> <span class="token punctuation">(</span>username<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="9"></td><td><pre>            log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">"***********用户名为null,非法用户，┭┮﹏┭┮"</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="10"></td><td><pre>            exchange<span class="token punctuation">.</span><span class="token function">getResponse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setStatusCode</span><span class="token punctuation">(</span><span class="token class-name">HttpStatus</span><span class="token punctuation">.</span>NOT_ACCEPTABLE<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="11"></td><td><pre>            <span class="token keyword">return</span> exchange<span class="token punctuation">.</span><span class="token function">getResponse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setComplete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="12"></td><td><pre>        <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="13"></td><td><pre>        <span class="token keyword">return</span> chain<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span>exchange<span class="token punctuation">)</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="14"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="15"></td><td><pre></pre></td></tr><tr><td data-num="16"></td><td><pre>    <span class="token annotation punctuation">@Override</span></pre></td></tr><tr><td data-num="17"></td><td><pre>    <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span></pre></td></tr><tr><td data-num="18"></td><td><pre>        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span></pre></td></tr><tr><td data-num="19"></td><td><pre>    <span class="token punctuation">&#125;</span></pre></td></tr><tr><td data-num="20"></td><td><pre><span class="token punctuation">&#125;</span></pre></td></tr></table></figure><p>然后启动服务，即可，因为过滤器通过 @COmponet 已经加入到容器了</p>
<p>如果没有带 username</p>
<p>直接报错</p>

      <div class="tags">
          <a href="/blog/tags/%E5%88%86%E5%B8%83%E5%BC%8F/" rel="tag"><i class="ic i-tag"></i> 分布式</a>
      </div>
  </div>

   <footer>

    <div class="meta">
  <span class="item">
    <span class="icon">
      <i class="ic i-calendar-check"></i>
    </span>
    <span class="text">更新于</span>
    <time title="修改时间：2020-12-24 14:53:09" itemprop="dateModified" datetime="2020-12-24T14:53:09+08:00">2020-12-24</time>
  </span>
  <span id="2020/12/24/gateway/" class="item leancloud_visitors" data-flag-title="gateway" title="阅读次数">
      <span class="icon">
        <i class="ic i-eye"></i>
      </span>
      <span class="text">阅读次数</span>
      <span class="leancloud-visitors-count"></span>
      <span class="text">次</span>
  </span>
</div>

      
<div class="reward">
  <button><i class="ic i-heartbeat"></i> 赞赏</button>
  <p>请我喝[茶]~(￣▽￣)~*</p>
  <div id="qr">
      
      <div>
        <img data-src="/blog/images/wechatpay.png" alt="杜朝宇 微信支付">
        <p>微信支付</p>
      </div>
      
      <div>
        <img data-src="/blog/images/alipay.png" alt="杜朝宇 支付宝">
        <p>支付宝</p>
      </div>
      
      <div>
        <img data-src="/blog/images/paypal.png" alt="杜朝宇 贝宝">
        <p>贝宝</p>
      </div>
  </div>
</div>

      

<div id="copyright">
<ul>
  <li class="author">
    <strong>本文作者： </strong>杜朝宇 <i class="ic i-at"><em>@</em></i>凡尘一叶
  </li>
  <li class="link">
    <strong>本文链接：</strong>
    <a href="http://fyduyu.gitee.io/blog/2020/12/24/gateway/" title="gateway">http://fyduyu.gitee.io/blog/2020/12/24/gateway/</a>
  </li>
  <li class="license">
    <strong>版权声明： </strong>本站所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC9kZWVkLnpo"><i class="ic i-creative-commons"><em>(CC)</em></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

  </footer>

</article>

  </div>
  

<div class="post-nav">
    <div class="item left">
      

  <a href="/blog/2020/12/24/Hystrix/" itemprop="url" rel="prev" data-background-image="https:&#x2F;&#x2F;tva4.sinaimg.cn&#x2F;mw690&#x2F;6833939bly1gicm0fdw5cj20zk0m8hdt.jpg" title="Hystrix">
  <span class="type">上一篇</span>
  <span class="category"><i class="ic i-flag"></i> SpringCloud</span>
  <h3>Hystrix</h3>
  </a>

    </div>
    <div class="item right">
      

  <a href="/blog/2020/12/24/Spring%20Config+Bus/" itemprop="url" rel="next" data-background-image="https:&#x2F;&#x2F;tva4.sinaimg.cn&#x2F;mw690&#x2F;6833939bly1gipeyhsblkj20zk0m81kx.jpg" title="Spring Config+Bus">
  <span class="type">下一篇</span>
  <span class="category"><i class="ic i-flag"></i> SpringCloud</span>
  <h3>Spring Config+Bus</h3>
  </a>

    </div>
</div>

  
  <div class="wrap" id="comments"></div>


        </div>
        <div id="sidebar">
          

<div class="inner">

  <div class="panels">
    <div class="inner">
      <div class="contents panel pjax" data-title="文章目录">
          <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.</span> <span class="toc-text"> 介绍</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8gateway"><span class="toc-number">2.</span> <span class="toc-text"> 使用 GateWay</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#pom"><span class="toc-number">2.1.</span> <span class="toc-text"> pom</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">2.2.</span> <span class="toc-text"> 配置文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BB%E5%90%AF%E5%8A%A8%E7%B1%BB"><span class="toc-number">2.3.</span> <span class="toc-text"> 主启动类</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%92%88%E5%AF%B9pay%E6%A8%A1%E5%9D%97%E8%AE%BE%E7%BD%AE%E8%B7%AF%E7%94%B1"><span class="toc-number">2.4.</span> <span class="toc-text"> 针对 pay 模块，设置路由</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%A7%8B%E6%B5%8B%E8%AF%95"><span class="toc-number">2.5.</span> <span class="toc-text"> 开始测试</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E7%BC%96%E7%A0%81%E6%96%B9%E5%BC%8F%E9%85%8D%E7%BD%AE"><span class="toc-number">2.6.</span> <span class="toc-text"> 通过编码方式配置</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E9%87%8D%E6%9E%84"><span class="toc-number">3.</span> <span class="toc-text"> 重构:</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9gateway%E6%A8%A1%E5%9D%97%E7%9A%84%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">3.1.</span> <span class="toc-text"> 修改 GateWay 模块的配置文件:</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#pridicate%E6%96%AD%E8%A8%80"><span class="toc-number">4.</span> <span class="toc-text"> Pridicate 断言:</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%9A%E8%BF%87%E6%97%B6%E9%97%B4%E5%8C%B9%E9%85%8D"><span class="toc-number">4.1.</span> <span class="toc-text"> 通过时间匹配</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%9A%E8%BF%87-cookie%E5%92%8C%E8%AF%B7%E6%B1%82%E5%A4%B4%E7%AD%89%E7%AD%89-%E5%8C%B9%E9%85%8D"><span class="toc-number">4.2.</span> <span class="toc-text"> 通过 Cookie 和请求头等等 匹配</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#filter%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">5.</span> <span class="toc-text"> Filter 过滤器:</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">5.1.</span> <span class="toc-text"> 生命周期:</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%A7%8D%E7%B1%BB"><span class="toc-number">5.2.</span> <span class="toc-text"> 种类:</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">5.3.</span> <span class="toc-text"> 自定义过滤器:</span></a></li></ol></li></ol>
      </div>
      <div class="related panel pjax" data-title="系列文章">
        <ul>
          <li><a href="/blog/2020/12/24/Hystrix/" rel="bookmark" title="Hystrix">Hystrix</a></li><li class="active"><a href="/blog/2020/12/24/gateway/" rel="bookmark" title="gateway">gateway</a></li><li><a href="/blog/2020/12/24/Spring%20Config+Bus/" rel="bookmark" title="Spring Config+Bus">Spring Config+Bus</a></li><li><a href="/blog/2020/12/24/Spring%20Cloud%20Stream/" rel="bookmark" title="Spring Cloud Stream">Spring Cloud Stream</a></li><li><a href="/blog/2020/12/24/Spring%20Cloud%20sleuth/" rel="bookmark" title="Spring Cloud sleuth">Spring Cloud sleuth</a></li>
        </ul>
      </div>
      <div class="overview panel" data-title="站点概览">
        <div class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <img class="image" itemprop="image" alt="杜朝宇"
      data-src="/blog/images/touxiang.jpg">
  <p class="name" itemprop="name">杜朝宇</p>
  <div class="description" itemprop="description">个人博客&编程笔记</div>
</div>

<nav class="state">
    <div class="item posts">
      <a href="/blog/archives/">
        <span class="count">8</span>
        <span class="name">文章</span>
      </a>
    </div>
    <div class="item categories">
      <a href="/blog/categories/">
        <span class="count">4</span>
        <span class="name">分类</span>
      </a>
    </div>
    <div class="item tags">
      <a href="/blog/tags/">
        <span class="count">2</span>
        <span class="name">标签</span>
      </a>
    </div>
</nav>

<div class="social">
</div>

<ul class="menu">
  
    
  <li class="item">
    <a href="/blog/" rel="section"><i class="ic i-home"></i>首页</a>
  </li>

    
  <li class="item">
    <a href="/blog/about/" rel="section"><i class="ic i-user"></i>关于</a>
  </li>

        
  <li class="item dropdown">
      <a href="javascript:void(0);"><i class="ic i-feather"></i>文章</a>
    <ul class="submenu">

        
  <li class="item">
    <a href="/blog/archives/" rel="section"><i class="ic i-list-alt"></i>归档</a>
  </li>

        
  <li class="item">
    <a href="/blog/categories/" rel="section"><i class="ic i-th"></i>分类</a>
  </li>

        
  <li class="item">
    <a href="/blog/tags/" rel="section"><i class="ic i-tags"></i>标签</a>
  </li>

  </ul>
        
  <li class="item dropdown">
      <a href="javascript:void(0);"><i class="ic i-magic"></i>链环</a>
    <ul class="submenu">

        
  <li class="item">
    <a href="/blog/friends/" rel="section"><i class="ic i-heart"></i>友链</a>
  </li>

  </ul>

</ul>

      </div>
    </div>
  </div>

  <ul id="quick">
    <li class="prev pjax">
        <a href="/blog/2020/12/24/Hystrix/" rel="prev" title="上一篇"><i class="ic i-chevron-left"></i></a>
    </li>
    <li class="up"><i class="ic i-arrow-up"></i></li>
    <li class="down"><i class="ic i-arrow-down"></i></li>
    <li class="next pjax">
        <a href="/blog/2020/12/24/Spring%20Config+Bus/" rel="next" title="下一篇"><i class="ic i-chevron-right"></i></a>
    </li>
    <li class="percent"></li>
  </ul>
</div>


        </div>
        <div class="dimmer"></div>
      </div>
    </main>
    <footer id="footer">
      <div class="inner">
        <div class="widgets">
          
<div class="rpost pjax">
  <h2>随机文章</h2>
  <ul>
      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/gateway/" title="gateway">gateway</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/Spring-Cloud-Alibaba/" title="分类于 Spring Cloud Alibaba">Spring Cloud Alibaba</a>
</div>

    <span><a href="/blog/2020/12/24/Nacos/" title="Cloud Alibaba Nacos">Cloud Alibaba Nacos</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/%E5%BC%80%E6%BA%90%E9%A1%B9%E7%9B%AE%E7%AC%94%E8%AE%B0/" title="分类于 开源项目笔记">开源项目笔记</a>
</div>

    <span><a href="/blog/2020/12/23/moti%E5%BC%80%E6%BA%90%E5%AD%A6%E5%88%B0%E7%9A%84/" title="moti">moti</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Hystrix/" title="Hystrix">Hystrix</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Cloud%20sleuth/" title="Spring Cloud sleuth">Spring Cloud sleuth</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Cloud%20Stream/" title="Spring Cloud Stream">Spring Cloud Stream</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/java/" title="分类于 java">java</a>
</div>

    <span><a href="/blog/2020/12/23/Druid%E9%85%8D%E7%BD%AE/" title="Druid配置">Druid配置</a></span>
  </li>

      
  <li class="item">
    
<div class="breadcrumb">
<a href="/blog/categories/SpringCloud/" title="分类于 SpringCloud">SpringCloud</a>
</div>

    <span><a href="/blog/2020/12/24/Spring%20Config+Bus/" title="Spring Config+Bus">Spring Config+Bus</a></span>
  </li>

  </ul>
</div>
<div>
  <h2>最新评论</h2>
  <ul class="leancloud-recent-comment"></ul>
</div>

        </div>
        <div class="status">
  <div class="copyright">
    
    &copy; 2010 – 
    <span itemprop="copyrightYear">2020</span>
    <span class="with-love">
      <i class="ic i-sakura rotate"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">杜朝宇 @ blog</span>
  </div>
  <div class="count">
    <span class="post-meta-item-icon">
      <i class="ic i-chart-area"></i>
    </span>
    <span title="站点总字数">42k 字</span>

    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="ic i-coffee"></i>
    </span>
    <span title="站点阅读时长">38 分钟</span>
  </div>
  <div class="powered-by">
    基于 <span class="exturl" data-url="aHR0cHM6Ly9oZXhvLmlv">Hexo</span> & Theme.<span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2FtZWhpbWUvaGV4by10aGVtZS1zaG9rYQ==">Shoka</span>
  </div>
</div>

      </div>
    </footer>
  </div>
<script data-config type="text/javascript">
  var LOCAL = {
    path: '2020/12/24/gateway/',
    favicon: {
      show: "（●´3｀●）やれやれだぜ",
      hide: "(´Д｀)大変だ！"
    },
    search : {
      placeholder: "文章搜索",
      empty: "关于 「 ${query} 」，什么也没搜到",
      stats: "${time} ms 内找到 ${hits} 条结果"
    },
    valine: true,fancybox: true,copyright: '复制成功，转载请遵守 <i class="ic i-creative-commons"></i>BY-NC-SA 协议。',
    ignores : [
      function(uri) {
        return uri.includes('#');
      },
      function(uri) {
        return new RegExp(LOCAL.path+"$").test(uri);
      }
    ]
  };
</script>

<script src="https://cdn.polyfill.io/v2/polyfill.js"></script>

<script src="//cdn.jsdelivr.net/combine/npm/pace-js@1.0.2/pace.min.js,npm/pjax@0.2.8/pjax.min.js,npm/whatwg-fetch@3.4.0/dist/fetch.umd.min.js,npm/animejs@3.2.0/lib/anime.min.js,npm/algoliasearch@4/dist/algoliasearch-lite.umd.js,npm/instantsearch.js@4/dist/instantsearch.production.min.js,npm/lozad@1/dist/lozad.min.js,npm/quicklink@2/dist/quicklink.umd.js"></script>

<script src="/blog/js/app.js?v=0.2.5"></script>




</body>
</html>
